/**
 * MapReduce构造器
 */
var MapReduceBuilder = function () {
  var _mr = {};
  var self = this;
  self._setPrototype = function (key, val) {
    _mr[key] = val;
    return self;
  };

  self._build = function () {
    return _mr;
  };

  self._boolVal = function (args, def) {
    var val = def;
    if (args.length) {
      val = !!args[0];
    }
    return val;
  }
};

/**
 * 设置map函数
 * @param mapFunc
 */
MapReduceBuilder.prototype.map = function (mapFunc) {
  return this._setPrototype('map', mapFunc);
};

/**
 * 设置reduce函数
 * @param reduceFunc
 */
MapReduceBuilder.prototype.reduce = function (reduceFunc) {
  return this._setPrototype('reduce', reduceFunc);
};

/**
 * 输出到指定的集合中,对应mapreduce中out字段的值
 * @param out 集合名词
 */
MapReduceBuilder.prototype.out = function (out) {
  return this._setPrototype('out', out);
};

/**
 * 查询字段 对应mapreduce中query字段的值
 * @param query
 */
MapReduceBuilder.prototype.query = function (queryFields) {
  return this._setPrototype('query', queryFields);
};

/**
 * 返回结果集合的限制长度,对应mapreduce中limit字段的值
 * @param limit
 */
MapReduceBuilder.prototype.limit = function (limit) {
  return this._setPrototype('limit', limit);
};

/**
 * 是否作为临时数据,对应mapreduce中keeptemp字段的值
 * 默认false,如果不传值,调用该接口,会将该字段设为true,传值的话,就根据传过来的值进行设置
 */
MapReduceBuilder.prototype.keeptemp = function () {
  return this._setPrototype('keeptemp', this._boolVal(arguments, true));
};

/**
 * 最终输出的处理函数,对应mapreduce中finalize字段的值
 * @param finalFunc
 */
MapReduceBuilder.prototype.finalize = function (finalFunc) {
  return this._setPrototype('finalize', finalFunc);
};

/**
 * 设置导入到mapreduce的map/reduce/finalize函数的变量对象,对应mapreduce中scope字段的值
 * @param scope
 */
MapReduceBuilder.prototype.scope = function (scope) {
  return this._setPrototype('scope', scope);
};

MapReduceBuilder.prototype.verbose = function () {
  return this._setPrototype('verbose', this._boolVal(arguments, true));
};

MapReduceBuilder.prototype.jsMode = function () {
  return _setPrototype('jsMode', this._boolVal(arguments, true));
};

MapReduceBuilder.prototype.readPreference = function (rp) {
  return _setPrototype('readPreference', rp);
};

MapReduceBuilder.prototype.modelManager = function (manager) {
  return _setPrototype('manager', manager);
};

MapReduceBuilder.prototype.create = function () {
  return this._build();
};

module.exports = MapReduceBuilder;